package wsserver;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * Created by lijian on 2020/11/21.
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private SimpleServiceDemo simpleServiceDemo;

    @Autowired
    private AnotherServiceDemo anotherServiceDemo;


    @Bean(name = "cxfServlet")
    public ServletRegistrationBean<CXFServlet> dispatcherServlet(){
        // WS服务默认发布地址为 http://127.0.0.1:8080/services/...
        return new ServletRegistrationBean(new CXFServlet(), "/services/*" );
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), simpleServiceDemo);
        // http://127.0.0.1:8080/services/ws/simpleService
        endpoint.publish("/ws/simpleService");
        return endpoint;
    }

    @Bean
    public Endpoint anotherEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), anotherServiceDemo);
        // http://127.0.0.1:8080/services/ws/anotherService
        endpoint.publish("/ws/anotherService");
        return endpoint;
    }
}
